<?php

namespace app\admin\controller;

use service\FormBuilder;
use service\UtilService as Util;
use service\JsonService as Json;
use service\UploadService as Upload;
use think\Request;
use think\Url;
use app\merchant\model\merchant\MerchantMenus as MenusModel;

/**
 * 菜单管理控制器
 * Class SystemMenus
 * @package app\admin\controller\system
 */
class MerchantMenus extends AuthController
{

    public function rules($id='')
    {
        if($id) $menu=MenusModel::get($id);
        $form=[];
        $form[]=FormBuilder::text('menu_name','按钮名称',isset($menu) ? $menu->menu_name : '');
        $form[]=FormBuilder::select('pid','父级id',isset($menu) ? $menu->pid : '')->setOptions(function(){
            $list =MenusModel::sortMenusTier(MenusModel::all());
            $menus =[['value'=>0,'label'=>'顶级']];
            foreach ($list as $menu){
                $menus[] = ['value'=>$menu['id'],'label'=>$menu['html'].$menu['menu_name']];
            }
            return $menus;
        })->filterable(1)->multiple(0);
        $form[]=FormBuilder::select('module','模块名','merchant')->setOptions([['label'=>'商户总后台','value'=>'merchant']]);
        $form[]=FormBuilder::text('controller','控制器名',isset($menu) ? $menu->controller : '');
        $form[]=FormBuilder::text('action','方法名',isset($menu) ? $menu->action : '');
        $form[]=FormBuilder::text('params','参数',isset($menu) ? (is_array($menu->params) ? implode(',',$menu->params) :  $menu->params ) : '')->placeholder('举例:a/123/b/234');
        $form[]=FormBuilder::frameInputOne('icon','图标',Url::build('admin/widget.widgets/icon',array('fodder'=>'icon')),isset($menu) ? $menu->icon : '')->icon('ionic');
        $form[]=FormBuilder::number('sort','排序',isset($menu) ? $menu->sort : 0);
        $form[]=FormBuilder::radio('is_show','是否显示',isset($menu) ? $menu->is_show : 1)
            ->options([['label'=>'隐藏','value'=>0],['label'=>'显示','value'=>1]]);
        return FormBuilder::create(Url::build($id ? 'update' : 'save',['id'=>$id]),$form);
    }

    public function upload()
    {
        $res = Upload::Image('file','config');
        if(!$res->status) return Json::fail($res->error);
        $thumbPath = Upload::thumb($res->dir);
        return Json::successful('图片上传成功!',['name'=>$res->fileInfo->getSaveName(),'url'=>Upload::pathToUrl($thumbPath)]);
    }

    public function attribute()
    {
        $limit = 15;
        $total = MenusModel::count();
        $head = ['id'=>'编号','pid'=>'上级菜单','menu_name'=>'按钮名称','module'=>'模块','action'=>'方法','is_show'=>'是否显示','access'=>'管理员可用','_handle'=>['edit','del']];
        return Json::successful(compact('limit','total','head'));
    }

    public function page()
    {
        $limit = (int)$_GET['limit'];
        $first = (int)$_GET['first'];
        $menu = new MenusModel;
        $list = $menu->limit($first,$limit)->select();
        return Json::successful($list);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $params = Util::getMore([
            ['is_show',''],
            ['access',''],
            ['keyword','']
        ],$this->request);
        $this->assign(MenusModel::getAdminPage($params));
        $this->assign(compact('params'));
        return $this->fetch();
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $this->assign(['form'=>$this->rules()]);
        return $this->fetch('public/form-builder');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data =Util::postMore([
            'menu_name',
            'controller',
            ['module','admin'],
            ['action',''],
            ['icon',''],
            ['params',''],
            ['pid',0],
            ['sort',0],
            ['is_show',0]
        ]);
        if(!$data['menu_name']) return Json::fail('请输入按钮名称');
        MenusModel::set($data);
        return Json::successful('添加菜单成功!');
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $this->assign(['form'=>$this->rules((int)$id)]);
        return $this->fetch('public/form-builder');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = Util::postMore([
            'menu_name',
            'controller',
            ['module','admin'],
            'action',
            'params',
            'icon',
            ['sort',0],
            ['pid',0],
            ['is_show',0]],$request);
        if(!$data['menu_name']) return Json::fail('请输入按钮名称');
        if(!MenusModel::get($id)) return Json::fail('编辑的记录不存在!');
        MenusModel::edit($data,$id);
        return Json::successful('修改成功!');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = MenusModel::delMenu($id);
        if(!$res)
            return Json::fail(MenusModel::getErrorInfo('删除失败,请稍候再试!'));
        else
            return Json::successful('删除成功!');
    }

    public function edit_content($id)
    {
        $this->assign(['field'=>'action','action'=>Url::build('change_field',['id'=>$id,'field'=>'action'])]);
        return $this->fetch();
    }
}
